#ifndef DSHAREDPOINTER_HPP
#define DSHAREDPOINTER_HPP

#include "../HDCore/DType.h"

template <class T>
class DSharedPointer
{
	friend class DPrinter;
public:
	explicit DSharedPointer(T* data);
	DSharedPointer(const DSharedPointer<T>& pointer);
	~DSharedPointer();

	DSizeType useCount() const { return *count_; }
	DSharedPointer<T>& operator=(const DSharedPointer<T>& pointer);
	bool operator==(const DSharedPointer<T>& pointer);
	bool operator!=(const DSharedPointer<T>& pointer) { return !(*this == pointer); }
	T& operator*() { return *data_; }
	T* operator->() { return data_; }

private:
	DSizeType* count_;
	T* data_;
};

/********************************* ʵ�� *********************************/

template <class T>
DSharedPointer<T>::DSharedPointer(T* data)
	: data_(data)
	, count_(new DSizeType(1))
{

}

template <class T>
DSharedPointer<T>::DSharedPointer(const DSharedPointer<T>& pointer)
{
	*this = pointer;
}

template <class T>
DSharedPointer<T>::~DSharedPointer()
{
	if (--*count_ == 0)
	{
		if (nullptr != data_)
			delete data_;
	}
}

template <class T>
DSharedPointer<T>& DSharedPointer<T>::operator=(const DSharedPointer<T>& pointer)
{
	++*pointer.count_;
	if (--*count_ == 0)
	{
		if (nullptr != data_)
			delete data_;
	}
	data_ = pointer.data_;
	return *this;
}

template <class T>
bool DSharedPointer<T>::operator==(const DSharedPointer<T>& pointer)
{
	return data_ == pointer.data_;
}

#endif  // DSHAREDPOINTER_HPP
